package com.ss.isc.web.manage.system.controller;

import com.ss.isc.data.system.client.IOrganizationRegionService;
import com.ss.isc.data.system.common.dto.UserPermission;
import com.ss.isc.data.system.common.model.OrganizationRegion;
import com.ss.isc.data.system.common.model.Region;
import com.ss.isc.util.constant.CacheConstant;
import com.ss.isc.util.jedis.JedisUtil;
import com.ss.isc.web.manage.baseinfo.controller.BaseController;
import com.ss.response.ResponseEntity;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping({"/system/organization"})
public class OrganizationManageController extends BaseController {

    @Resource
    private IOrganizationRegionService oService;
    @Resource
    private JedisUtil jedisUtil;

    @RequestMapping(value = {"/list"}, method = {RequestMethod.POST})
    public ResponseEntity<List<OrganizationRegion>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody OrganizationRegion dto, BindingResult bindingResult) throws Exception {
        try {
            ResponseEntity<List<OrganizationRegion>> resp = validite(bindingResult);


            List<OrganizationRegion> oList = this.oService.getList(dto);

            resp.setData(oList);
            return resp;
        } catch (Exception e) {
            this.logger.error(e.toString(), e);
            throw e;
        }
    }


    @RequestMapping(value = {"/inserts"}, method = {RequestMethod.POST})
    public ResponseEntity<Map<String, String>> update(HttpServletRequest request, HttpServletResponse response, @RequestBody OrganizationRegion dto, BindingResult bindingResult) throws Exception {
        try {
            ResponseEntity<Map<String, String>> resp = validite(bindingResult);
            this.oService.update(dto);

            this.jedisUtil.del(new String[]{CacheConstant.REDIS_KEY_USERPERMISSION});
            return resp;
        } catch (Exception e) {
            this.logger.error(e.toString(), e);
            throw e;
        }
    }


    @RequestMapping(value = {"/delt"}, method = {RequestMethod.POST})
    public ResponseEntity<List<Region>> delt(HttpServletRequest request, HttpServletResponse response, @RequestBody OrganizationRegion dto, BindingResult bindingResult) throws Exception {
        try {
            ResponseEntity<List<Region>> resp = validite(bindingResult);

            List<Region> list = this.oService.getTree();

            resp.setData(list);
            return resp;
        } catch (Exception e) {
            this.logger.error(e.toString(), e);
            throw e;
        }
    }


    @RequestMapping(value = {"/findUserPermission"}, method = {RequestMethod.POST})
    public ResponseEntity<List<UserPermission>> findUserPermission(HttpServletRequest request, BindingResult bindingResult) throws Exception {
        try {
            ResponseEntity<List<UserPermission>> resp = validite(bindingResult);
            resp.setData(this.oService.findUserPermission());
            return resp;
        } catch (Exception e) {
            this.logger.error(e.toString(), e);
            throw e;
        }
    }

}
